package iso3.pt.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import iso3.pt.dao.IncorrectPasswordException;
import iso3.pt.dao.UserNotFoundException;
import iso3.pt.model.Alumno;
import iso3.pt.model.Profesor;
import iso3.pt.service.PtDaoService;
import iso3.pt.service.PtService;

import java.util.Map;


/**
 * <p> Validate a user login. </p>
 */
public class LoginAction  extends ActionSupport {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private PtService service = new PtDaoService();

	@SuppressWarnings("unchecked")
	public String execute() throws Exception 
	{       
		if (getUsername().equals("") || getPassword().equals("")) 
		{
			addActionError(getText("errors.invalid.login.specify"));
			return INPUT;
		}
		else
		{
			@SuppressWarnings("rawtypes")
			Map session = ActionContext.getContext().getSession();
			
			try
			{
				if (getSelector().equals("1"))
				{
					Alumno alumno = service.loginAlumno(Integer.valueOf(getUsername()), getPassword());
					session.put("alumno", alumno);
					return "listStudentSubjects";
				}
				else
				{
					Profesor profesor = service.loginProfesor(Integer.valueOf(getUsername()), getPassword());
					session.put("profesor", profesor);
					return "listLecturerSubjects";
				}
			} 
			catch (UserNotFoundException e)
			{
				addActionError(getText("errors.invalid.login.user"));
	            return ERROR;
			}
			catch (NumberFormatException e)
			{
				addActionError(getText("errors.invalid.login.user"));
	            return ERROR;
			}
	        catch (IncorrectPasswordException e)
	        {
	        	addActionError(getText("errors.invalid.login.password"));
	            return ERROR;
	        }		
		}
	}


    // ---- Username property ----

    /**
     * <p>Field to store User username.</p>
     * <p/>
     */
    private String username = null;


    /**
     * <p>Provide User username.</p>
     *
     * @return Returns the User username.
     */
    public String getUsername() {
        return username;
    }

    /**
     * <p>Store new User username</p>
     *
     * @param value The username to set.
     */
    public void setUsername(String value) {
        username = value;
    }

    // ---- Username property ----

    /**
     * <p>Field to store User password.</p>
     * <p/>
     */
    private String password = null;


    /**
     * <p>Provide User password.</p>
     *
     * @return Returns the User password.
     */
    public String getPassword() {
        return password;
    }

    /**
     * <p>Store new User password</p>
     *
     * @param value The password to set.
     */
    public void setPassword(String value) {
        password = value;
    }
    
	private String selector = null;
    
	public String getSelector() 
	{
		return selector;
	}

	public void setSelector(String selector) 
	{
		this.selector = selector;
	}

}
